package session

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/sessions"
)

// 创建一个基于 Cookie 的会话存储对象
var store = sessions.NewCookieStore([]byte("哈哈哈")) // 括号中的是 用于加密和验证会话数据的密钥
var sessionName = "haha"                           // 用作会话的名称

func SetSession(c *gin.Context, name string, id int64) error {
	session, _ := store.Get(c.Request, sessionName)
	session.Values["name"] = name
	session.Values["id"] = id

	session.Options.MaxAge = 21600 // 6个小时
	return session.Save(c.Request, c.Writer)
}

func GetSession(c *gin.Context) map[interface{}]interface{} {
	session, _ := store.Get(c.Request, sessionName)
	fmt.Printf("%+v", session.Values)
	return session.Values
}

func FlushSession(c *gin.Context) error {
	session, _ := store.Get(c.Request, sessionName)
	fmt.Printf("%+v", session.Values)
	session.Values["name"] = ""
	session.Values["id"] = int64(0)
	return session.Save(c.Request, c.Writer)
}
